<template>
  <div class="auth-container">
    <div class="auth-card">
      <h2 class="auth-title">
        <span>{{ pageTitle }}</span>
        <div class="title-decorator"></div>
      </h2>

      <form @submit.prevent="register" class="auth-form">
        <div class="form-group">
          <select v-model="data.role" id="role" class="custom-select">
            <option value="student">学生</option>
            <option value="company">企业</option>
          </select>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <i class="el-icon-user-solid icon"></i>
            <input
              type="text"
              v-model="data.username"
              id="username"
              placeholder="账号"
              class="custom-input"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <i class="el-icon-lock icon"></i>
            <input
              type="password"
              v-model="data.password"
              id="password"
              placeholder="密码"
              class="custom-input"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <i class="el-icon-lock icon"></i>
            <input
              type="password"
              v-model="data.confirmPassword"
              id="confirmPassword"
              placeholder="确认密码"
              class="custom-input"
              required
            />
          </div>
        </div>

        <div class="button-group">
          <button type="submit" class="primary-button">注册</button>
          <button
            type="button"
            @click="goToLogin"
            class="secondary-button"
          >
            返回登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>
<script>
import { ElMessage } from 'element-plus';
import { reactive } from 'vue';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const data = reactive({
      role: 'student',
      username: '',
      password: '',
      confirmPassword: '',
    });

    const router = useRouter();

    const register = () => {
      if (data.password !== data.confirmPassword) {
        ElMessage.error('两次输入的密码不一致');
        return;
      }

      fetch('http://localhost:8080/api/register', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          role: data.role,
          username: data.username,
          password: data.password,
        }),
      })
        .then((response) => {
          if (!response.ok) {
            throw new Error('网络响应异常');
          }
          return response.json();
        })
        .then((res) => {
          if (res.code === 200) {
            ElMessage.success('注册成功');
            router.push('/login');
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch((error) => {
          ElMessage.error('注册失败，请检查网络');
          console.error(error);
        });
    };

    const goToLogin = () => {
      router.push('/login');
    };

    return {
      data,
      register,
      goToLogin,
      pageTitle: '注册',
    };
  },
};
</script>
<style scoped>
.auth-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  align-items: center;
  justify-content: flex-end; /* 右对齐 */
  padding: 20px;
  box-sizing: border-box;
}

.auth-card {
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  padding: 40px;
  animation: fadeIn 0.5s ease-out;
  /* 确保卡片不会超出屏幕 */
  margin-left: auto;
  margin-right: 20px;
}

.auth-title {
  font-family: 'Poppins', sans-serif;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 20px;
  position: relative;
  text-align: center;
}

.title-decorator {
  width: 60px;
  height: 4px;
  background: #3498db;
  margin: 10px auto 0;
  border-radius: 2px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group label {
  font-size: 14px;
  color: #34495e;
  margin-bottom: 6px;
  font-weight: 500;
}

.custom-select,
.custom-input {
  width: 90%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  height: 48px;
  transition: all 0.3s ease;
}

.custom-select:focus,
.custom-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.input-wrapper {
  position: relative;
}

.icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 18px;
}

.button-group {
  display: flex;
  gap: 12px;
  flex-direction: column;
}

.primary-button {
  background: linear-gradient(135deg, #3498db, #2980b9);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.primary-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0,0,0,0.15);
}

.secondary-button {
  background: #ecf0f1;
  color: #34495e;
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.secondary-button:hover {
  background: #dcedf5;
  color: #2980b9;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 500px) {
  .auth-card {
    padding: 30px 20px;
  }
  
  .custom-input,
  .custom-select {
    font-size: 13px;
    padding: 10px;
  }
}
</style>